package castledefense.gui;

import twl.global.input.GlobalEvent;
import twl.global.widget.GlobalPanel;
import twl.global.widget.LabelFonts;

import castledefense.data.BuildingData;
import de.matthiasmann.twl.GUI;
import de.matthiasmann.twl.Widget;

public class BuildingPanel extends GlobalPanel{
	
	private GUIMainGame stateGui;
	
	private BuildingButton tower1 = new BuildingButton("Tower1", BuildingData.tower1, this);
	private BuildingButton tower2 = new BuildingButton("Tower2", BuildingData.tower2, this);
	private BuildingButton destroy = new BuildingButton("Destroy", BuildingData.destroy, this);
	
	private LabelFonts moneyLabel;
	
	public BuildingPanel(GUIMainGame gui){
		this.stateGui = gui;
		
		
		setTheme("panelTheme");
		
		//Add building button
		add(tower1);
		tower1.setPosition(10, 39);
		add(tower2);
		tower2.setPosition(65, 39);
		add(destroy);
		destroy.setPosition(40, 104);
		destroy.setSize(96, 30);
		
		Widget line = new Widget();
		line.setTheme("fillRectBlack");
		line.setSize(159, 2);
		line.setPosition(10, 95);
		add(line);
		
		moneyLabel = new LabelFonts("Money " + BuildingData.money + "$");
		moneyLabel.setTheme("labelTheme");
		moneyLabel.setFont("arialBold20");
		moneyLabel.setPosition(10, 20);
		add(moneyLabel);
		
		stateGui.getTWL().getGlobalGUI().addGlobalEventListener(this);
	}
	
	public GUIMainGame getStateGUI(){
		return stateGui;
	}
	
	@Override
	protected void paintWidget(GUI gui){
		moneyLabel.setText("Money " + BuildingData.money + "$");
	}
	
	public void setSelection(int num) {
		boolean select = true;
		if(BuildingData.building == num){
			select = false;
			BuildingData.building = BuildingData.noBuilding;
		}else{
			// Set the selection in Data
			BuildingData.building = num;
		}
		
		// Turn all button to false
		tower1.setSelected(false);
		tower2.setSelected(false);
		destroy.setSelected(false);
		
		if(select){
			stateGui.getState().getMap().unSelectBuildings();
			stateGui.getState().getMap().unSelectUnit();
		}

		// Turn the selected one to true
		if(select){
			if (num == BuildingData.tower1) {
				tower1.setSelected(true);
			} else if (num == BuildingData.tower2) {
				tower2.setSelected(true);
			} else if (num == BuildingData.destroy) {
				destroy.setSelected(true);
			}
		}
		
	}
	
	
	@Override
	public void handleGlobalEvent(GlobalEvent evt){
		if(evt.getType() == GlobalEvent.Type.KEY_PRESSED){
			// tower1
			if(evt.getKeyCode() == GlobalEvent.KEY_1){
				setSelection(BuildingData.tower1);
			}
			// tower2
			if (evt.getKeyCode() == GlobalEvent.KEY_2) {
				setSelection(BuildingData.tower2);
			}
			// eraser (keyboard �)
			if (evt.getKeyCode() == GlobalEvent.KEY_BACKSLASH) {
				setSelection(BuildingData.destroy);
			}
		}
		
		if(evt.getType() == GlobalEvent.Type.MOUSE_CLICKED){
			if(evt.getMouseButton() == GlobalEvent.MOUSE_RBUTTON){
				setSelection(BuildingData.noBuilding);
			}
		}
	}

}
